<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use App\Scopes\IsDelScope;

class ProductModel extends BaseModel {

    protected static function booted()
    {
        parent::booted();
    }

    protected $table = "dtrd_ent_shc_rd_product";
    protected $primaryKey = "data_id";

    const CREATED_AT = 'create_time';
    const UPDATED_AT = null;

    protected $casts = [
        'create_time' => 'datetime:Y-m-d H:i:s',
    ];

    protected $hidden = ['is_del', 'product_status', 'product_service_id'];
    protected $fillable = [];
    protected $appends = [
        'product_category_text'
    ];

    const PRODUCT_CATEGORY = [
        1 => '监测设备',
        // 2 => '监测设备',
        3 => '服务',
        4 => '营养餐包',
    ];

    public function getProductPriceAttribute($value)
    {
        return sprintf('%s', $value/100);
    }

    public function getProductDiscountPriceAttribute($value)
    {
        return sprintf('%s', $value/100);
    }

    public function getProductVipPriceAttribute($value)
    {
        return sprintf('%s', $value/100);
    }

    public function getProductBannerAttribute($value)
    {
        return !empty($value) ? explode(',', $value) : [];
    }

    public function getProductCategoryTextAttribute()
    {
        switch ($this->product_category) {
            case 1:
            case 2:
                return '监测设备';
            case 3:
                return '服务';
            case 4:
                return '营养餐包';
            default:
                return '';
        }
    }

    public function service()
    {
        return $this->hasOne(ServiceModel::class, 'data_id', 'product_service_id');
    }

    public function sales()
    {
        return $this->hasMany(OrderProductModel::class, 'product_id', 'data_id');
    }
}
